package itheima.zuoye.Test4;

import java.util.*;
import java.util.stream.Collectors;

public class Test {
    //本题综合单列集合,双列集合,键盘输入,Stream流操作
    //1.键盘录入3个学生信息,存储到学生对象(姓名,年龄).然后添加到ArrayList集
    //合中.
    //2.键盘录入3个居住地信息,添加到另一个集合ArrayList;
    //3.把两个list集合中的数据收集到同一个map集合中,键是学生对对象,值是居
    //住地址.
    //4.要求map集合中不能存在相同的学生信息.并按照学生年龄降序排列
    //5.使用Stream流输出集合中所有姓张的人信息
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        List<Student> students = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入第" + (i + 1) + "个学生的信息:");

            String input = sc.nextLine();
            String[] parts = input.split(" ");
            String name = parts[0];
            int age = Integer.parseInt(parts[1]);
            students.add(new Student(name, age));
        }

        List<String> addresses = new ArrayList<>();
        for (int j = 0; j < 3; j++) {
            System.out.println("请输入第" + (j + 1) + "个学生的居住地:");
            String address = sc.nextLine();
            addresses.add(address);
        }
        Map<Student, String> map = new LinkedHashMap<>();
        for (int j = 0; j < students.size(); j++) {
            Student student = students.get(j);
            String address = addresses.get(j);
            map.put(student, address);
        }

        map = map.entrySet().stream()
                .sorted(Map.Entry.<Student, String>comparingByKey(Comparator.comparing(Student::getAge).reversed()))
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (existing, replacement) -> existing,
                        LinkedHashMap::new
                ));

        map.entrySet().stream()
                .filter(entry -> entry.getKey().getName().startsWith("张"))
                .forEach(entry -> System.out.println("姓名: " + entry.getKey().getName() + ", 年龄: " + entry.getKey().getAge() + ", 地址: " + entry.getValue()));


    }


}

